//绑定电子卡
var app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
var countnocardtime = null;
var countwithcardtime = null;
Page({
  data: {
    last_nocardtime: '',
    last_cardtime: '',
    is_show: true,
    is_show_card: true,
    noCardMesgCode: '',
    noCardID: "",
    noCardTel: '',
    isGet: false,
    errmesg: "请核对后重新输入",
    showErrPop: false,
    listType: 1, //1默认我没有卡，2有卡
    showExamplePop: false,
    existCardNumber: '',
    existCardPwd: '',
    existCardTel: '',
    existCardMesgCode: '',
    isBindNoCard:false,
    isBindExistCard:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.fromType = options.type;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  countNoCardTime() {
    var that = this;
    if (that.data.last_nocardtime == 0) {
      that.setData({
        is_show: true,
        isGet: false
      })
    } else if (that.data.last_nocardtime > 0) {
      that.setData({
        is_show: false,
        isGet: true
      })
    }
    countnocardtime = setTimeout(function () {
      that.data.last_nocardtime = parseInt(that.data.last_nocardtime) - 1;
      that.setData({
        last_nocardtime: that.data.last_nocardtime
      })
      that.countNoCardTime(that.data.last_nocardtime)
    }, 1000)
  },
  sendNoCardCheckCode() {
    if (this.data.noCardTel != '') {
      this.setData({
        isGet: true
      })
      server.postJSON(common.basePath + 'card/verifybind/', {
        "mobile": this.data.noCardTel,
        "type": 4
      }, res => {
        if (res.data.errno == "0") {
          this.setData({
            last_nocardtime: 60
          });
          clearTimeout(countnocardtime);
          this.countNoCardTime();
        } else {
          this.setData({
            isGet: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else {
      common.showErrorToast('请输入手机号！');
    }
  },
  getNoCardMesgCode(e) {
    this.setData({
      noCardMesgCode: e.detail.value
    })
  },
  getNoCardTel(e) {
    this.setData({
      noCardTel: e.detail.value.replace(/\s+/g, '')
    })
  },
  getNoCardID(e) {
    this.setData({
      noCardID: e.detail.value
    })
  },
  bindNoCard() {
    if (this.data.noCardMesgCode != '' && this.data.noCardTel != '' && this.data.noCardID != '') {
      this.setData({
        isBindNoCard:true
      })
      server.postJSON(common.basePath + 'card/bind/', {
        "mobile": this.data.noCardTel,
        "identity": this.data.noCardID,
        "verify": this.data.noCardMesgCode,
        "city": app.globalData.city
      }, res => {
        setTimeout(() => {
          this.setData({
            isBindNoCard:false
          })
        }, 200);
        if (res.data.errno == "0") {
          var data = res.data.data;
          common.showErrorToast('绑定成功！');
          var that = this;
          setTimeout(function () {
            if (that.data.fromType == 'account') {
              app.globalData.isRefreshAccountList=true;
            } 
            wx.navigateBack();
          }, 500)
        } else {
          this.setData({
            isGet: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else if (this.data.noCardID == '') {
      common.showErrorToast('请输入渠道/证件信息！');
    } else if (this.data.noCardTel == '') {
      common.showErrorToast('请输入手机号！');
    } else if (this.data.noCardMesgCode == '') {
      common.showErrorToast('请输入验证码！');
    }
  },
  cancelPop() {
    this.setData({
      showErrPop: false
    })
    wx.navigateBack()
  },
  reWrite() {
    this.setData({
      showErrPop: false,
      is_show: true,
      mesgCode: ''
    })
  },
  switchTab(e) {
    this.setData({
      listType: e.currentTarget.dataset.id
    })
  },
  toggleExample() {
    this.setData({
      showExamplePop: !this.data.showExamplePop
    })
  },
  countCardTime() {
    var that = this;
    if (that.data.last_cardtime == 0) {
      that.setData({
        is_show_card: true,
        isGet_card: false
      })
    } else if (that.data.last_cardtime > 0) {
      that.setData({
        is_show_card: false,
        isGet_card: true
      })
    }
    countwithcardtime = setTimeout(function () {
      that.data.last_cardtime = parseInt(that.data.last_cardtime) - 1;
      that.setData({
        last_cardtime: that.data.last_cardtime
      })
      that.countCardTime(that.data.last_cardtime)
    }, 1000)
  },
  sendExistCardCheckCode() {
    if (this.data.existCardTel != '') {
      this.setData({
        isGet_card: true
      })
      server.postJSON(common.basePath + 'card/verifybind/', {
        "mobile": this.data.existCardTel,
        "type": 4
      }, res => {
        if (res.data.errno == "0") {
          this.setData({
            last_cardtime: 60
          });
          clearTimeout(countwithcardtime);
          this.countCardTime();
        } else {
          this.setData({
            isGet_card: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else {
      common.showErrorToast('请输入手机号！');
    }
    
  },
  getExistCardCheckCode(e) {
    this.setData({
      existCardMesgCode: e.detail.value
    })
  },
  getExistCardNumber(e){
    this.setData({
      existCardNumber: e.detail.value
    })
  },
  getExistCardTel(e){
    this.setData({
      existCardTel: e.detail.value
    })
  },
  getExistCardPwd(e){
    this.setData({
      existCardPwd: e.detail.value
    })
  },
  bindExistCard() {
    if (this.data.existCardTel != '' && this.data.existCardNumber != '' && this.data.existCardPwd != ''&& this.data.existCardMesgCode != '') {
      this.setData({
        isBindExistCard:true
      })
      server.postJSON(common.basePath + 'card/bindAnonymity/', {
        "mobile": this.data.existCardTel,
        "card_no": this.data.existCardNumber,
        "pwd": this.data.existCardPwd,
        "verify": this.data.existCardMesgCode
      }, res => {
        setTimeout(() => {
          this.setData({
            isBindExistCard:false
          })
        }, 200);
        if (res.data.errno == "0") {
          var data = res.data.data;
          common.showErrorToast('绑定成功！');
          var that = this;
          setTimeout(function () {
            if (that.data.fromType == 'account') {
              app.globalData.isRefreshAccountList=true;
            } 
            wx.navigateBack();
          }, 500)
        } else {
          this.setData({
            isGet: false
          })
          common.showErrorToast(res.data.errmsg);
        }
      })
    } else if (this.data.existCardNumber == '') {
      common.showErrorToast('请输入礼卡卡号！');
    } else if (this.data.existCardPwd == '') {
      common.showErrorToast('请输入礼卡密码！');
    } else if (this.data.existCardTel == '') {
      common.showErrorToast('请输入手机号！');
    }else if (this.data.existCardMesgCode == '') {
      common.showErrorToast('请输入验证码！');
    }
  }
})